/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.kj.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.sys.entity.EmpUser;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.entity.UserDataScope;
import com.jeesite.modules.sys.service.EmpUserService;
import com.jeesite.modules.sys.service.UserService;
import com.jeesite.modules.sys.service.support.EmpUserServiceSupport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.kj.entity.KjAdminRegion;
import com.jeesite.modules.kj.service.KjAdminRegionService;

import java.util.List;

/**
 * kj_admin_regionController
 * @author hzy
 * @version 2021-08-08
 */
@Controller
@RequestMapping(value = "${adminPath}/kj/kjAdminRegion")
public class KjAdminRegionController extends BaseController {

	@Autowired
	private KjAdminRegionService kjAdminRegionService;

	@Autowired
	private UserService userService;

	@Autowired
	private EmpUserService empUserService;
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public KjAdminRegion get(String userCode, String cityCode, String cityName, boolean isNewRecord) {
		KjAdminRegion kjAdminRegion = new KjAdminRegion();
		kjAdminRegion.setUserCode(userCode);
		kjAdminRegion.setCityCode(cityCode);
		kjAdminRegion.setCityName(cityName);
		kjAdminRegion.setIsNewRecord(isNewRecord);
		return kjAdminRegionService.getAndValid(kjAdminRegion);
	}

	/**
	 * 查询列表
	 */
	@RequiresPermissions("kj:kjAdminRegion:view")
	@RequestMapping(value = {"list", ""})
	public String list(KjAdminRegion kjAdminRegion, Model model) {
		model.addAttribute("kjAdminRegion", kjAdminRegion);
		return "modules/kj/kjAdminRegionList";
	}

	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("kj:kjAdminRegion:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<KjAdminRegion> listData(KjAdminRegion kjAdminRegion, HttpServletRequest request, HttpServletResponse response) {
		kjAdminRegion.setPage(new Page<>(request, response));
		Page<KjAdminRegion> page = kjAdminRegionService.findPage(kjAdminRegion);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("kj:kjAdminRegion:view")
	@RequestMapping(value = "form")
	public String form(KjAdminRegion kjAdminRegion, Model model) {
		model.addAttribute("kjAdminRegion", kjAdminRegion);
		return "modules/kj/kjAdminRegionForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("kj:kjAdminRegion:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated KjAdminRegion kjAdminRegion) {
		kjAdminRegionService.save(kjAdminRegion);
		return renderResult(Global.TRUE, text("保存管理员地区成功！"));
	}

	/**
	 * 删除数据
	 */
	@RequiresPermissions("kj:kjAdminRegion:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(KjAdminRegion kjAdminRegion) {
		kjAdminRegionService.delete(kjAdminRegion);
		return renderResult(Global.TRUE, text("删除管理员地区成功！"));
	}


	/**
	 * 用户授权数据权限
	 */
	@RequiresPermissions("kj:kjAdminRegion:edit")
	@RequestMapping(value = "formRegion")
	public String formRegion(EmpUser empUser, Model model, HttpServletRequest request) {
		empUser = empUserService.get(empUser.getId());
		KjAdminRegion regionScope = new KjAdminRegion();
		regionScope.setUserCode(empUser.getUserCode());
		List<KjAdminRegion> regionScopeList = kjAdminRegionService.findList(regionScope);
		model.addAttribute("regionScopeList", regionScopeList);
		model.addAttribute("empUser", empUser);
		return "modules/kj/kjAdminRegionFormScope";
	}

	/**
	 * 保存用户授权数据权限
	 */
	@RequiresPermissions("kj:kjAdminRegion:edit")
	@RequestMapping(value = "saveRegionDataScope")
	@ResponseBody
	public String saveAuthDataScope(EmpUser empUser, HttpServletRequest request) {
		KjAdminRegion where = new KjAdminRegion();
		where.setUserCode(empUser.getUserCode());
		kjAdminRegionService.deleteUserCode(where);

		List<UserDataScope> scopeList = empUser.getUserDataScopeList();
		for (UserDataScope scope: scopeList
			 ) {
			KjAdminRegion region = new KjAdminRegion();
			region.setUserCode(empUser.getUserCode());
			region.setCityCode(scope.getCtrlData());
			region.setCityName(scope.getCtrlType());
			region.setIsNewRecord(true);
			kjAdminRegionService.save(region);
		}
		return renderResult(Global.TRUE, text("用户分配地区权限成功"));
	}

}
